package com.micangcaihang.micang.adapter;

import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.micangcaihang.common.utils.ConvertUtils;
import com.micangcaihang.common.utils.TimeUtils;
import com.micangcaihang.micang.R;
import com.micangcaihang.micang.entity.FTaskPracticalUserInfoEntity;
import com.micangcaihang.micang.entity.MatesEntity;
import com.micangcaihang.micang.view.RecyclerMatesLayout;

import java.util.List;

/**
 * <pre>
 *     author : Fandy
 *     e-mail : fandayongfly@gmail.com
 *     time   : 2017/08/01
 *     update : 2017/08/01
 *     desc   : 世纪用款人信息适配器
 *     version: 1.0
 * </pre>
 */

public class FTaskPracticalUserInfoAdapter extends BaseQuickAdapter<FTaskPracticalUserInfoEntity, BaseViewHolder> {
    private TextChangeListener1 mTextChangeListener1;
    private TextChangeListener2 mTextChangeListener2;
    private TextChangeListener3 mTextChangeListener3;

    public FTaskPracticalUserInfoAdapter(@LayoutRes int layoutResId, @Nullable List<FTaskPracticalUserInfoEntity> data) {
        super(layoutResId, data);
    }


    @Override
    protected void convert(BaseViewHolder helper, FTaskPracticalUserInfoEntity entity) {
        int position = helper.getLayoutPosition();
        helper.setText(R.id.tv_big_title, String.format(mContext.getString(R.string.format_practical_user_info_big_title), ConvertUtils.StringToNumber(position + 1)));
        if (helper.getLayoutPosition() == 0) {
            helper.setVisible(R.id.tv_add, false);
        } else {
            helper.setVisible(R.id.tv_add, false);
        }
        helper.addOnClickListener(R.id.tv_add);
        helper.addOnClickListener(R.id.lly_item_customer_sex);
        helper.addOnClickListener(R.id.lly_item_marriage_status);
        helper.addOnClickListener(R.id.lly_item_certificate_type);
        helper.addOnClickListener(R.id.lly_item_peason_credit);
        helper.addOnClickListener(R.id.lly_item_peason_crediless);
        helper.addOnClickListener(R.id.lly_item_peason_enforced);
        helper.addOnClickListener(R.id.lly_item_query_time);
        helper.addOnClickListener(R.id.lly_item_relationship_type);
        helper.addOnClickListener(R.id.btn_delete);

        EditText et_customer_name = helper.getView(R.id.et_item_customer_name);
        EditText et_certificate_number = helper.getView(R.id.et_item_certificate_number);
        EditText et_mobile_phone = helper.getView(R.id.et_item_mobile_phone);

        et_customer_name.removeTextChangedListener((TextChangeListener1) et_customer_name.getTag());
        et_certificate_number.removeTextChangedListener((TextChangeListener2) et_certificate_number.getTag());
        et_mobile_phone.removeTextChangedListener((TextChangeListener3) et_mobile_phone.getTag());

        ////共有人姓名
        helper.setText(R.id.et_item_customer_name, entity.getCustomer_name());
        //证件号码
        helper.setText(R.id.et_item_certificate_number, entity.getCertificate_number());
        //查询时间
        try {
            String queryTime = TimeUtils.millis2StringDay(Long.parseLong(entity.getQuery_time()) * 1000);
            helper.setText(R.id.tv_item_query_time, queryTime);
        } catch (Exception e) {
            e.printStackTrace();
            helper.setText(R.id.tv_item_query_time, "");
        }
        //联系电话
        helper.setText(R.id.et_item_mobile_phone, entity.getMobile_phone());

        mTextChangeListener1 = new TextChangeListener1(entity);
        et_customer_name.addTextChangedListener(mTextChangeListener1);
        et_customer_name.setTag(mTextChangeListener1);

        mTextChangeListener2 = new TextChangeListener2(entity);
        et_certificate_number.addTextChangedListener(mTextChangeListener2);
        et_certificate_number.setTag(mTextChangeListener2);

        mTextChangeListener3 = new TextChangeListener3(entity);
        et_mobile_phone.addTextChangedListener(mTextChangeListener3);
        et_mobile_phone.setTag(mTextChangeListener3);

        RecyclerMatesLayout matesLayout = helper.getView(R.id.mates_item_layout);
        List<MatesEntity> mates = entity.getMates();
        if ("1".equals(entity.getMarriage_status())) {
            matesLayout.setType(1);
            matesLayout.setVisibility(View.GONE);
        } else if ("2".equals(entity.getMarriage_status())) {
            matesLayout.setType(2);
            matesLayout.setVisibility(View.VISIBLE);
        } else if ("3".equals(entity.getMarriage_status())) {
            matesLayout.setType(3);
            matesLayout.setVisibility(View.VISIBLE);
        } else if ("4".equals(entity.getMarriage_status())) {
            matesLayout.setType(4);
            matesLayout.setVisibility(View.VISIBLE);
        } else if ("5".equals(entity.getMarriage_status())) {
            matesLayout.setType(5);
            matesLayout.setVisibility(View.VISIBLE);
        } else {
            matesLayout.setType(0);
            matesLayout.setVisibility(View.GONE);
        }
        matesLayout.setMatesEntities(mates);
        FTaskPracticalUserInfoEntity.ChooseListBean choose_list = entity.getChoose_list();
        //性别
        helper.setText(R.id.tv_item_customer_sex, choose_list.getCustomer_sex());
        ////证件类型
        helper.setText(R.id.tv_item_certificate_type, choose_list.getCertificate_type());
        //婚姻
        helper.setText(R.id.tv_item_marriage_status, choose_list.getMarriage_status());
        // //征信状况
        helper.setText(R.id.tv_item_peason_credit, choose_list.getPeason_credit());
        //是否失信
        helper.setText(R.id.tv_item_peason_crediless, choose_list.getPeason_crediless());
        //是否被执行
        helper.setText(R.id.tv_item_peason_enforced, choose_list.getPeason_enforced());
        //与借款人关系
        helper.setText(R.id.tv_item_relationship_type, choose_list.getRelationship_type());
    }

    class TextChangeListener1 implements TextWatcher {
        FTaskPracticalUserInfoEntity entity;

        public TextChangeListener1(FTaskPracticalUserInfoEntity entity) {
            this.entity = entity;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            entity.setCustomer_name(s.toString());
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    }

    class TextChangeListener2 implements TextWatcher {
        FTaskPracticalUserInfoEntity entity;

        public TextChangeListener2(FTaskPracticalUserInfoEntity entity) {
            this.entity = entity;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            entity.setCertificate_number(s.toString());
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    }

    class TextChangeListener3 implements TextWatcher {

        FTaskPracticalUserInfoEntity entity;

        public TextChangeListener3(FTaskPracticalUserInfoEntity entity) {

            this.entity = entity;

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            entity.setMobile_phone(s.toString());
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    }

}
